/*
 * @(#)DmsDocumentDetail.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.dms.bean.DmsDocumentDetail;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;

/**
  DmsDocumentDetailDAObject.java

  This class is the data access bean for table "DMS_DOCUMENT_DETAIL".

  @author      Zoe Shum
  @company     DCIVision Limited
  @creation date   14/08/2003
  @version     $Revision: 1.27.4.1 $
*/

public class DmsDocumentDetailDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.27.4.1 $";

  public static final String TABLE_NAME = "DMS_DOCUMENT_DETAIL";

  public DmsDocumentDetailDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  
  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("DOCUMENT_ID");
    this.vecDBColumn.add("USER_DEFINED_FIELD_ID");
    this.vecDBColumn.add("USER_DEFINED_FIELD_DETAIL_ID");
    this.vecDBColumn.add("FIELD_VALUE");
    this.vecDBColumn.add("DATE_VALUE");
    this.vecDBColumn.add("NUMERIC_VALUE");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  
  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.USER_DEFINED_FIELD_ID, A.USER_DEFINED_FIELD_DETAIL_ID, A.FIELD_VALUE, A.DATE_VALUE, A.NUMERIC_VALUE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_DOCUMENT_DETAIL A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          DmsDocumentDetail tmpDmsDocumentDetail = new DmsDocumentDetail();
          tmpDmsDocumentDetail.setID(getResultSetInteger(rs, "ID"));
          tmpDmsDocumentDetail.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsDocumentDetail.setUserDefinedFieldID(getResultSetInteger(rs, "USER_DEFINED_FIELD_ID"));
          tmpDmsDocumentDetail.setUserDefinedFieldDetailID(getResultSetInteger(rs, "USER_DEFINED_FIELD_DETAIL_ID"));
          tmpDmsDocumentDetail.setFieldValue(getResultSetString(rs, "FIELD_VALUE"));
          tmpDmsDocumentDetail.setDateValue(getResultSetTimestamp(rs, "DATE_VALUE"));
          tmpDmsDocumentDetail.setNumericValue(getResultSetDouble(rs, "NUMERIC_VALUE"));
          tmpDmsDocumentDetail.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsDocumentDetail.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsDocumentDetail.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsDocumentDetail.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsDocumentDetail.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsDocumentDetail.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsDocumentDetail.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getCreatorID()));
          tmpDmsDocumentDetail.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getUpdaterID()));
          return(tmpDmsDocumentDetail);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.USER_DEFINED_FIELD_ID, A.USER_DEFINED_FIELD_DETAIL_ID, A.FIELD_VALUE, A.DATE_VALUE, A.NUMERIC_VALUE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_DOCUMENT_DETAIL A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        /*
        if (searchForm.isSearchable()) {
          String searchField = searchForm.getBasicSearchField();
          if (searchField.indexOf(".") < 0) {
            searchField = "A." + searchField;
          }
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }*/
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          DmsDocumentDetail tmpDmsDocumentDetail = new DmsDocumentDetail();
          tmpDmsDocumentDetail.setID(getResultSetInteger(rs, "ID"));
          tmpDmsDocumentDetail.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsDocumentDetail.setUserDefinedFieldID(getResultSetInteger(rs, "USER_DEFINED_FIELD_ID"));
          tmpDmsDocumentDetail.setUserDefinedFieldDetailID(getResultSetInteger(rs, "USER_DEFINED_FIELD_DETAIL_ID"));
          tmpDmsDocumentDetail.setFieldValue(getResultSetString(rs, "FIELD_VALUE"));
          tmpDmsDocumentDetail.setDateValue(getResultSetTimestamp(rs, "DATE_VALUE"));
          tmpDmsDocumentDetail.setNumericValue(getResultSetDouble(rs, "NUMERIC_VALUE"));
          tmpDmsDocumentDetail.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsDocumentDetail.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsDocumentDetail.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsDocumentDetail.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsDocumentDetail.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsDocumentDetail.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsDocumentDetail.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getCreatorID()));
          tmpDmsDocumentDetail.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getUpdaterID()));
          tmpDmsDocumentDetail.setRecordCount(totalNumOfRecord);
          tmpDmsDocumentDetail.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpDmsDocumentDetail);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  
  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.USER_DEFINED_FIELD_ID, A.USER_DEFINED_FIELD_DETAIL_ID, A.FIELD_VALUE, A.DATE_VALUE, A.NUMERIC_VALUE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_DOCUMENT_DETAIL A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsDocumentDetail tmpDmsDocumentDetail = new DmsDocumentDetail();
          tmpDmsDocumentDetail.setID(getResultSetInteger(rs, "ID"));
          tmpDmsDocumentDetail.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsDocumentDetail.setUserDefinedFieldID(getResultSetInteger(rs, "USER_DEFINED_FIELD_ID"));
          tmpDmsDocumentDetail.setUserDefinedFieldDetailID(getResultSetInteger(rs, "USER_DEFINED_FIELD_DETAIL_ID"));
          tmpDmsDocumentDetail.setFieldValue(getResultSetString(rs, "FIELD_VALUE"));
          tmpDmsDocumentDetail.setDateValue(getResultSetTimestamp(rs, "DATE_VALUE"));
          tmpDmsDocumentDetail.setNumericValue(getResultSetDouble(rs, "NUMERIC_VALUE"));
          tmpDmsDocumentDetail.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsDocumentDetail.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsDocumentDetail.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsDocumentDetail.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsDocumentDetail.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsDocumentDetail.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsDocumentDetail.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getCreatorID()));
          tmpDmsDocumentDetail.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getUpdaterID()));
          result.add(tmpDmsDocumentDetail);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsDocumentDetail tmpDmsDocumentDetail = (DmsDocumentDetail)((DmsDocumentDetail)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   DMS_DOCUMENT_DETAIL(ID, DOCUMENT_ID, USER_DEFINED_FIELD_ID, USER_DEFINED_FIELD_DETAIL_ID, FIELD_VALUE, DATE_VALUE, NUMERIC_VALUE, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpDmsDocumentDetail.getDocumentID());
        setPrepareStatement(preStat, 3, tmpDmsDocumentDetail.getUserDefinedFieldID());
        setPrepareStatement(preStat, 4, tmpDmsDocumentDetail.getUserDefinedFieldDetailID());
        setPrepareStatement(preStat, 5, tmpDmsDocumentDetail.getFieldValue());
        setPrepareStatement(preStat, 6, tmpDmsDocumentDetail.getDateValue());
        setPrepareStatement(preStat, 7, tmpDmsDocumentDetail.getNumericValue());
//        setPrepareStatement(preStat, 8, tmpDmsDocumentDetail.getUpdateDate());
        setPrepareStatement(preStat, 8, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 9, new Integer(0));
        setPrepareStatement(preStat, 10, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 11, currTime);
        setPrepareStatement(preStat, 12, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 13, currTime);
        preStat.executeUpdate();
        tmpDmsDocumentDetail.setID(nextID);
        tmpDmsDocumentDetail.setCreatorID(sessionContainer.getUserRecordID());
        tmpDmsDocumentDetail.setCreateDate(currTime);
        tmpDmsDocumentDetail.setUpdaterID(sessionContainer.getUserRecordID());
        tmpDmsDocumentDetail.setUpdateDate(currTime);
        tmpDmsDocumentDetail.setUpdateCount(new Integer(0));
        tmpDmsDocumentDetail.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getCreatorID()));
        tmpDmsDocumentDetail.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getUpdaterID()));
        return(tmpDmsDocumentDetail);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsDocumentDetail tmpDmsDocumentDetail = (DmsDocumentDetail)((DmsDocumentDetail)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE DMS_DOCUMENT_DETAIL ");
        sqlStat.append("SET  DOCUMENT_ID=?, USER_DEFINED_FIELD_ID=?, USER_DEFINED_FIELD_DETAIL_ID=?, FIELD_VALUE=?, DATE_VALUE=?, NUMERIC_VALUE=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpDmsDocumentDetail.getDocumentID());
        setPrepareStatement(preStat, 2, tmpDmsDocumentDetail.getUserDefinedFieldID());
        setPrepareStatement(preStat, 3, tmpDmsDocumentDetail.getUserDefinedFieldDetailID());
        setPrepareStatement(preStat, 4, tmpDmsDocumentDetail.getFieldValue());
        setPrepareStatement(preStat, 5, tmpDmsDocumentDetail.getDateValue());
        setPrepareStatement(preStat, 6, tmpDmsDocumentDetail.getNumericValue());
        setPrepareStatement(preStat, 7, new Integer(tmpDmsDocumentDetail.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 8, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 9, currTime);
        setPrepareStatement(preStat, 10, tmpDmsDocumentDetail.getID());
        setPrepareStatement(preStat, 11, tmpDmsDocumentDetail.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpDmsDocumentDetail.setUpdaterID(sessionContainer.getUserRecordID());
          tmpDmsDocumentDetail.setUpdateDate(currTime);
          tmpDmsDocumentDetail.setUpdateCount(new Integer(tmpDmsDocumentDetail.getUpdateCount().intValue() + 1));
          tmpDmsDocumentDetail.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getCreatorID()));
          tmpDmsDocumentDetail.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getUpdaterID()));
          return(tmpDmsDocumentDetail);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsDocumentDetail tmpDmsDocumentDetail = (DmsDocumentDetail)((DmsDocumentDetail)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   DMS_DOCUMENT_DETAIL ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpDmsDocumentDetail.getID());
        setPrepareStatement(preStat, 2, tmpDmsDocumentDetail.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpDmsDocumentDetail);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    DmsDocumentDetail tmpDmsDocumentDetail = (DmsDocumentDetail)this.oldValue;

    if (tmpDmsDocumentDetail != null) {
      oldValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getID()));
      oldValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getDocumentID()));
      oldValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getUserDefinedFieldID()));
      oldValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getUserDefinedFieldDetailID()));
      oldValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getFieldValue()));
      oldValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getDateValue()));
      oldValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getUpdateDate()));
    }

    tmpDmsDocumentDetail = (DmsDocumentDetail)obj;
    if (tmpDmsDocumentDetail != null) {
      newValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getID()));
      newValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getDocumentID()));
      newValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getUserDefinedFieldID()));
      newValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getUserDefinedFieldDetailID()));
      newValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getFieldValue()));
      newValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getDateValue()));
      newValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpDmsDocumentDetail.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
  
  
 /**
  * Gets the list of the dmsDocumentDetail objects by document ID
  * 
  * @param documentID
  * @return List of the dmsDocumentDetail object
  * @throws ApplicationException
  * <pre>
  * 
  *            ApplicationException with the following message codes will be thrown
  *            ErrorConstant.ErrorConstant.DB_GENERAL_ERROR - General caught SQLException
  *            ErrorConstant.DB_SELECT_ERROR - All kind of caught Exception
  *  
  * </pre>
  */
  public synchronized List getListByDocumentID(Integer documentID) throws ApplicationException {
      return getListByDocumentID(documentID,GlobalConstant.STATUS_ACTIVE);
  }
  
  
  /**
   * Gets the list of the dmsDocumentDetail objects by document ID and record status
   * 
   * @param documentID
   * @param recordStatus the document record status
   * @return list of the dmsDocumentDetail objects
   * @throws ApplicationException
   */
  public synchronized List getListByDocumentID(Integer documentID, String recordStatus) throws ApplicationException {
      PreparedStatement ps = null;
      ResultSet rs = null;
      StringBuffer sqlStat = new StringBuffer();
      List result = new ArrayList();
      synchronized(dbConn) {
        try {
          sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.USER_DEFINED_FIELD_ID, A.USER_DEFINED_FIELD_DETAIL_ID, A.FIELD_VALUE, A.DATE_VALUE, A.NUMERIC_VALUE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
          sqlStat.append("FROM   DMS_DOCUMENT_DETAIL A ");
          sqlStat.append("WHERE  A.DOCUMENT_ID = ? AND A.RECORD_STATUS = ? ");
          sqlStat.append("ORDER BY A.USER_DEFINED_FIELD_DETAIL_ID ");
          ps = dbConn.prepareStatement(sqlStat.toString());
          setPrepareStatement(ps, 1, documentID);
          setPrepareStatement(ps, 2, recordStatus);
          rs = ps.executeQuery();
          while (rs.next()) {
            DmsDocumentDetail tmpDmsDocumentDetail = new DmsDocumentDetail();
            tmpDmsDocumentDetail.setID(getResultSetInteger(rs, "ID"));
            tmpDmsDocumentDetail.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
            tmpDmsDocumentDetail.setUserDefinedFieldID(getResultSetInteger(rs, "USER_DEFINED_FIELD_ID"));
            tmpDmsDocumentDetail.setUserDefinedFieldDetailID(getResultSetInteger(rs, "USER_DEFINED_FIELD_DETAIL_ID"));
            tmpDmsDocumentDetail.setFieldValue(getResultSetString(rs, "FIELD_VALUE"));
            tmpDmsDocumentDetail.setDateValue(getResultSetTimestamp(rs, "DATE_VALUE"));
            tmpDmsDocumentDetail.setNumericValue(getResultSetDouble(rs, "NUMERIC_VALUE"));
            tmpDmsDocumentDetail.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
            tmpDmsDocumentDetail.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
            tmpDmsDocumentDetail.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
            tmpDmsDocumentDetail.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
            tmpDmsDocumentDetail.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
            tmpDmsDocumentDetail.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
            tmpDmsDocumentDetail.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getCreatorID()));
            tmpDmsDocumentDetail.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getUpdaterID()));
            result.add(tmpDmsDocumentDetail);
          }
        } catch (ApplicationException appEx) {
          throw appEx;
        } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
        } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
        } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { ps.close(); } catch (Exception ignore) {} finally { ps = null; }
        }
      }
      return result;
    }
    
    /**
     * Gets list of the active and inactive dmsDocumentDetail objects by document ID
     * 
     * @param documentID the dmsDocument ohject
     * @return list of the dmsDocumentDetail objects
     * @throws ApplicationException
     */
    public synchronized List getListByDocumentIDActiveInactive(Integer documentID) throws ApplicationException {
      PreparedStatement ps = null;
      ResultSet rs = null;
      StringBuffer sqlStat = new StringBuffer();
      List result = new ArrayList();
      synchronized(dbConn) {
	try {
	  sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.USER_DEFINED_FIELD_ID, A.USER_DEFINED_FIELD_DETAIL_ID, A.FIELD_VALUE, A.DATE_VALUE, A.NUMERIC_VALUE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
	  sqlStat.append("FROM   DMS_DOCUMENT_DETAIL A ");
	  sqlStat.append("WHERE  A.DOCUMENT_ID = ?  ");
	  sqlStat.append("ORDER BY A.USER_DEFINED_FIELD_DETAIL_ID ");
	  ps = dbConn.prepareStatement(sqlStat.toString());
	  setPrepareStatement(ps, 1, documentID);
	
	  rs = ps.executeQuery();
	  while (rs.next()) {
	    DmsDocumentDetail tmpDmsDocumentDetail = new DmsDocumentDetail();
	    tmpDmsDocumentDetail.setID(getResultSetInteger(rs, "ID"));
	    tmpDmsDocumentDetail.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
	    tmpDmsDocumentDetail.setUserDefinedFieldID(getResultSetInteger(rs, "USER_DEFINED_FIELD_ID"));
	    tmpDmsDocumentDetail.setUserDefinedFieldDetailID(getResultSetInteger(rs, "USER_DEFINED_FIELD_DETAIL_ID"));
	    tmpDmsDocumentDetail.setFieldValue(getResultSetString(rs, "FIELD_VALUE"));
	    tmpDmsDocumentDetail.setDateValue(getResultSetTimestamp(rs, "DATE_VALUE"));
	    tmpDmsDocumentDetail.setNumericValue(getResultSetDouble(rs, "NUMERIC_VALUE"));
	    tmpDmsDocumentDetail.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
	    tmpDmsDocumentDetail.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
	    tmpDmsDocumentDetail.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
	    tmpDmsDocumentDetail.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
	    tmpDmsDocumentDetail.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
	    tmpDmsDocumentDetail.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
	    tmpDmsDocumentDetail.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getCreatorID()));
	    tmpDmsDocumentDetail.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getUpdaterID()));
	    result.add(tmpDmsDocumentDetail);
	  }
	} catch (ApplicationException appEx) {
	  throw appEx;
	} catch (SQLException sqle) {
	  log.error(sqle, sqle);
	  throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
	} catch (Exception e) {
	  log.error(e, e);
	  throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
	} finally {
	  try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
	  try { ps.close(); } catch (Exception ignore) {} finally { ps = null; }
	}
      }
      return result;
    }
    
    /**
     * Gets the dmsDocumentDetail object by document ID and user defined field detail ID
     * 
     * @param documentID the dmsDocument object id
     * @param udfDetailID the user defined field Detail ID
     * @param recordStatus 
     * @return abstract object of dmsDocumentDetail 
     * @throws ApplicationException
     * @see com.dcivision.framework.bean.SysUserDefinedIndexDetail
     */
    public synchronized AbstractBaseObject getObjectByDocumentIDUDFDetailID(Integer documentID, Integer udfDetailID, String recordStatus) throws ApplicationException {
      PreparedStatement ps = null;
      ResultSet rs = null;
      StringBuffer sqlStat = new StringBuffer();
      List resultList = new ArrayList();
      DmsDocumentDetail tmpDmsDocumentDetail  = null;
      synchronized(dbConn) {
        try {
          sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.USER_DEFINED_FIELD_ID, A.USER_DEFINED_FIELD_DETAIL_ID, A.FIELD_VALUE, A.DATE_VALUE, A.NUMERIC_VALUE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
          sqlStat.append("FROM   DMS_DOCUMENT_DETAIL A ");
          sqlStat.append("WHERE  A.DOCUMENT_ID = ? AND A.USER_DEFINED_FIELD_DETAIL_ID = ? AND A.RECORD_STATUS = ? ");
          ps = dbConn.prepareStatement(sqlStat.toString());
          setPrepareStatement(ps, 1, documentID);
          setPrepareStatement(ps, 2, udfDetailID);
          setPrepareStatement(ps, 3, recordStatus);
          rs = ps.executeQuery();
          if (rs.next()) {
            tmpDmsDocumentDetail  = new DmsDocumentDetail();
            tmpDmsDocumentDetail.setID(getResultSetInteger(rs, "ID"));
            tmpDmsDocumentDetail.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
            tmpDmsDocumentDetail.setUserDefinedFieldID(getResultSetInteger(rs, "USER_DEFINED_FIELD_ID"));
            tmpDmsDocumentDetail.setUserDefinedFieldDetailID(getResultSetInteger(rs, "USER_DEFINED_FIELD_DETAIL_ID"));
            tmpDmsDocumentDetail.setFieldValue(getResultSetString(rs, "FIELD_VALUE"));
            tmpDmsDocumentDetail.setDateValue(getResultSetTimestamp(rs, "DATE_VALUE"));
            tmpDmsDocumentDetail.setNumericValue(getResultSetDouble(rs, "NUMERIC_VALUE"));
            tmpDmsDocumentDetail.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
            tmpDmsDocumentDetail.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
            tmpDmsDocumentDetail.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
            tmpDmsDocumentDetail.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
            tmpDmsDocumentDetail.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
            tmpDmsDocumentDetail.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
            tmpDmsDocumentDetail.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getCreatorID()));
            tmpDmsDocumentDetail.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getUpdaterID()));
          }
        } catch (ApplicationException appEx) {
          throw appEx;
        } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
        } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
        } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { ps.close(); } catch (Exception ignore) {} finally { ps = null; }
        }
      }

      return tmpDmsDocumentDetail;
    }
    
    
    /**
     * Delete the document detail by document id
     * 
     * @param DocumentID the dmsDocument object ID
     * @return No. of the record deleted
     * @throws ApplicationException
     */
    public synchronized Integer deleteByDocumentID(Integer DocumentID) throws ApplicationException {
      PreparedStatement preStat = null;
      StringBuffer sqlStat = new StringBuffer();

      int updateCnt = 0;
      synchronized(dbConn) {
        try {
          sqlStat.append("DELETE ");
          sqlStat.append("FROM   DMS_DOCUMENT_DETAIL ");
          sqlStat.append("WHERE  DOCUMENT_ID = ? ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

          setPrepareStatement(preStat, 1, DocumentID);
          updateCnt = preStat.executeUpdate();

        } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
        } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
        } finally {
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        }
      }
      return new Integer(updateCnt);
   }

    
   /**
    * Delete the document detail by user defined field ID
    * 
    * @param udfID user defined field ID
    * @return No. of record deleted
    * @throws ApplicationException
    * @see com.dcivision.framework.bean.SysUserDefinedIndex
    */
   public synchronized Integer deleteByUDFID(Integer udfID) throws
       ApplicationException {
     PreparedStatement preStat = null;
     StringBuffer sqlStat = new StringBuffer();

     int updateCnt = 0;
     synchronized (dbConn) {
       try {
         sqlStat.append("DELETE ");
         sqlStat.append("FROM   DMS_DOCUMENT_DETAIL ");
         sqlStat.append("WHERE  USER_DEFINED_FIELD_ID  = ? ");
         preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//log.debug("sql = " + sqlStat.toString());
         setPrepareStatement(preStat, 1, udfID);
         updateCnt = preStat.executeUpdate();

       } catch (SQLException sqle) {
         log.error(sqle, sqle);
         throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
       } catch (Exception e) {
         log.error(e, e);
         throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
       } finally {
         try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
       }
     }
     return new Integer(updateCnt);
   }

   /**
    * Gets the dmsDocumentDetail object by user defined field id
    *
    * @param userDefinedID the user defined field id
    * @return the dmsDocumentDetail object
    * @throws ApplicationException
    * @see com.dcivision.framework.bean.SysUserDefinedIndex 
    */
   public List getByUDFID(Integer userDefinedID) throws ApplicationException{
     PreparedStatement preStat=null;
     ResultSet rs = null;
     StringBuffer sqlStat = new StringBuffer();
     List result = new ArrayList();

     synchronized(dbConn){
       try{
         sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.USER_DEFINED_FIELD_ID, A.USER_DEFINED_FIELD_DETAIL_ID, A.FIELD_VALUE, A.DATE_VALUE, A.NUMERIC_VALUE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
         sqlStat.append("FROM DMS_DOCUMENT_DETAIL A, DMS_DOCUMENT D ");
         sqlStat.append("WHERE A.DOCUMENT_ID = D.ID AND A.USER_DEFINED_FIELD_ID=? AND D.RECORD_STATUS<>? ");
         preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
         this.setPrepareStatement(preStat, 1, userDefinedID);
         this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_INACTIVE);
         rs = preStat.executeQuery();
         while (rs.next()) {
            DmsDocumentDetail tmpDmsDocumentDetail = new DmsDocumentDetail();
            tmpDmsDocumentDetail.setID(getResultSetInteger(rs, "ID"));
            tmpDmsDocumentDetail.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
            tmpDmsDocumentDetail.setUserDefinedFieldID(getResultSetInteger(rs, "USER_DEFINED_FIELD_ID"));
            tmpDmsDocumentDetail.setUserDefinedFieldDetailID(getResultSetInteger(rs, "USER_DEFINED_FIELD_DETAIL_ID"));
            tmpDmsDocumentDetail.setFieldValue(getResultSetString(rs, "FIELD_VALUE"));
            tmpDmsDocumentDetail.setDateValue(getResultSetTimestamp(rs, "DATE_VALUE"));
            tmpDmsDocumentDetail.setNumericValue(getResultSetDouble(rs, "NUMERIC_VALUE"));
            tmpDmsDocumentDetail.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
            tmpDmsDocumentDetail.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
            tmpDmsDocumentDetail.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
            tmpDmsDocumentDetail.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
            tmpDmsDocumentDetail.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
            tmpDmsDocumentDetail.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
            tmpDmsDocumentDetail.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getCreatorID()));
            tmpDmsDocumentDetail.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getUpdaterID()));
            result.add(tmpDmsDocumentDetail);
          }

       }catch (SQLException sqle) {
         log.error(sqle, sqle);
         throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
       } catch (Exception e) {
         log.error(e, e);
         throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
       } finally {
         try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
       }

     }
     return result;
   }
   
   /**
    * Checks any existing record by specified user defined field id
    * 
    * @param userDefinedID User defined field ID
    * @return boolean true if there is record exists.
    * @throws ApplicationException
    */
   public boolean checkExistByUDFID(Integer userDefinedID) throws ApplicationException{
        PreparedStatement preStat=null;
        ResultSet rs = null;
        StringBuffer sqlStat = new StringBuffer();
        List result = new ArrayList();
        boolean hasRecord = false;
        synchronized(dbConn){
          try{
            sqlStat.append("SELECT DISTINCT 1 ");
            sqlStat.append("FROM DMS_DOCUMENT_DETAIL A, DMS_DOCUMENT D ");
            //Update By Jim Zhou		2006/10/10	EIP-269		delete " AND D.RECORD_STATUS<>? "
            //sqlStat.append("WHERE A.DOCUMENT_ID = D.ID AND A.USER_DEFINED_FIELD_ID=? AND D.RECORD_STATUS<>? ");
            sqlStat.append("WHERE A.DOCUMENT_ID = D.ID AND A.USER_DEFINED_FIELD_ID=? ");
            preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            this.setPrepareStatement(preStat, 1, userDefinedID);
            //this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_INACTIVE);
            rs = preStat.executeQuery();
            if (rs.next()) {
              hasRecord = true;
            }
            
          }catch (SQLException sqle) {
            log.error(sqle, sqle);
            throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
          } catch (Exception e) {
            log.error(e, e);
            throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
          } finally {
            try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
          }

        }
        return hasRecord;
      }
   
   /**
    * Deletes the dmsDocumentDetail record by ID
    * 
    * @param ID of DmsDocDetail ID
    * @return
    * @throws ApplicationException
    * */

   public synchronized void deleteByID(Integer ID) throws ApplicationException {
		     PreparedStatement preStat = null;
		     StringBuffer sqlStat = new StringBuffer();
	

		     synchronized(dbConn) {
			       try {
				         int updateCnt = 0;
				         sqlStat.append("DELETE ");
				         sqlStat.append("FROM   DMS_DOCUMENT_DETAIL ");
				         sqlStat.append("WHERE  ID=? ");
				         preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
				         this.setPrepareStatement(preStat, 1, ID);
				         updateCnt = preStat.executeUpdate();
				         if (updateCnt == 0) {
					           throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
				         } 
			      } catch (ApplicationException appEx) {
				        throw appEx;
			      } catch (SQLException sqle) {
				        log.error(sqle, sqle);
				        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
			      } catch (Exception e) {
				        log.error(e, e);
				        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
			      } finally {
				        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
			      }
		    }
	  }
   
  /**
   * Update the document profile value to newValue by specified field value and user defined detail ID
   * 
   * @param newValue new value for updated
   * @param cleanOptValue existing field value
   * @param udfDetailID the use defined detail ID
   * @throws ApplicationException
   */
  public synchronized void cleanProfileValue(String newValue, String cleanOptValue, Integer udfDetailID) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE DMS_DOCUMENT_DETAIL ");
        sqlStat.append("SET  FIELD_VALUE= ");
        if (Utility.isEmpty(newValue)){
          sqlStat.append("NULL ");
        }else{
          sqlStat.append("? ");
        }
        sqlStat.append("WHERE  FIELD_VALUE=? AND USER_DEFINED_FIELD_DETAIL_ID=?  ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int i=1;
        if (!Utility.isEmpty(newValue)){
          setPrepareStatement(preStat, i++, newValue);
        }
        setPrepareStatement(preStat, i++, cleanOptValue);
        setPrepareStatement(preStat, i++, udfDetailID);

        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          //throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } 
      //} catch (ApplicationException appEx) {
      //  throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Delete the document Detail by the user defined field detail id
   * 
   * @param udfDetailID the user defined field detail id
   * @throws ApplicationException
   * @see com.dcivision.framework.bean.SysUserDefinedIndexDetail  
   */
  public synchronized void deleteByUDFDetailID(Integer udfDetailID) throws ApplicationException {
       PreparedStatement preStat = null;
       StringBuffer sqlStat = new StringBuffer();

       int updateCnt = 0;
       synchronized (dbConn) {
         try {
           sqlStat.append("DELETE ");
           sqlStat.append("FROM   DMS_DOCUMENT_DETAIL ");
           sqlStat.append("WHERE  USER_DEFINED_FIELD_DETAIL_ID  = ? ");
           preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//  log.debug("sql = " + sqlStat.toString());
           setPrepareStatement(preStat, 1, udfDetailID);
           updateCnt = preStat.executeUpdate();

         } catch (SQLException sqle) {
           log.error(sqle, sqle);
           throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
         } catch (Exception e) {
           log.error(e, e);
           throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
         } finally {
           try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
         }
       }
       
     }
     
     
  /**
   * Insert new dmsDocuemnt Detail 
   * 
   * @param obj abstract object of dmsDocument detail
   * @throws ApplicationException
   */
  public synchronized void insertFieldNameOnly(AbstractBaseObject obj) throws ApplicationException {
     PreparedStatement preStat = null;
     StringBuffer sqlStat = new StringBuffer();
     DmsDocumentDetail tmpDmsDocumentDetail = (DmsDocumentDetail)((DmsDocumentDetail)obj).clone();


     synchronized(dbConn) {
       try {
         Integer nextID = getNextPrimaryID();
         Timestamp currTime = Utility.getCurrentTimestamp();
         sqlStat.append("INSERT ");
         sqlStat.append("INTO   DMS_DOCUMENT_DETAIL(ID, DOCUMENT_ID, USER_DEFINED_FIELD_ID, USER_DEFINED_FIELD_DETAIL_ID, FIELD_VALUE, DATE_VALUE, NUMERIC_VALUE, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
         sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
         preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
         setPrepareStatement(preStat, 1, nextID);
         setPrepareStatement(preStat, 2, tmpDmsDocumentDetail.getDocumentID());
         setPrepareStatement(preStat, 3, tmpDmsDocumentDetail.getUserDefinedFieldID());
         setPrepareStatement(preStat, 4, tmpDmsDocumentDetail.getUserDefinedFieldDetailID());
         setPrepareStatement(preStat, 5, tmpDmsDocumentDetail.getFieldValue());
         setPrepareStatement(preStat, 6, tmpDmsDocumentDetail.getDateValue());
         setPrepareStatement(preStat, 7, tmpDmsDocumentDetail.getNumericValue());
//         setPrepareStatement(preStat, 8, tmpDmsDocumentDetail.getUpdateDate());
         setPrepareStatement(preStat, 8, GlobalConstant.RECORD_STATUS_ACTIVE);
         setPrepareStatement(preStat, 9, new Integer(0));
         setPrepareStatement(preStat, 10, sessionContainer.getUserRecordID());
         setPrepareStatement(preStat, 11, currTime);
         setPrepareStatement(preStat, 12, sessionContainer.getUserRecordID());
         setPrepareStatement(preStat, 13, currTime);
         preStat.executeUpdate();
         tmpDmsDocumentDetail.setID(nextID);
         tmpDmsDocumentDetail.setCreatorID(sessionContainer.getUserRecordID());
         tmpDmsDocumentDetail.setCreateDate(currTime);
         tmpDmsDocumentDetail.setUpdaterID(sessionContainer.getUserRecordID());
         tmpDmsDocumentDetail.setUpdateDate(currTime);
         tmpDmsDocumentDetail.setUpdateCount(new Integer(0));
         tmpDmsDocumentDetail.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getCreatorID()));
         tmpDmsDocumentDetail.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getUpdaterID()));
         
       } catch (ApplicationException appEx) {
         throw appEx;
       } catch (SQLException sqle) {
         log.error(sqle, sqle);
         throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
       } catch (Exception e) {
         log.error(e, e);
         throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
       } finally {
         try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
       }
     }
   }
  
  /**
   * Gets the document detail object by user defined field ID
   * 
   * @param userDefinedID the user defined field ID
   * @return
   * @throws ApplicationException
   */
  public List getByUDFIDGroupByDocumentID(Integer userDefinedID) throws ApplicationException{
      PreparedStatement preStat=null;
      ResultSet rs = null;
      StringBuffer sqlStat = new StringBuffer();
      List result = new ArrayList();

      synchronized(dbConn){
        try{
          sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.USER_DEFINED_FIELD_ID, A.USER_DEFINED_FIELD_DETAIL_ID, A.FIELD_VALUE, A.DATE_VALUE, A.NUMERIC_VALUE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
          sqlStat.append("FROM DMS_DOCUMENT_DETAIL A, DMS_DOCUMENT D ");
          sqlStat.append("WHERE A.DOCUMENT_ID = D.ID AND A.USER_DEFINED_FIELD_ID=? AND D.RECORD_STATUS<>? ");
          sqlStat.append("GROUP BY DOCUMENT_ID ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          this.setPrepareStatement(preStat, 1, userDefinedID);
          this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_INACTIVE);
          rs = preStat.executeQuery();
          while (rs.next()) {
             DmsDocumentDetail tmpDmsDocumentDetail = new DmsDocumentDetail();
             tmpDmsDocumentDetail.setID(getResultSetInteger(rs, "ID"));
             tmpDmsDocumentDetail.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
             tmpDmsDocumentDetail.setUserDefinedFieldID(getResultSetInteger(rs, "USER_DEFINED_FIELD_ID"));
             tmpDmsDocumentDetail.setUserDefinedFieldDetailID(getResultSetInteger(rs, "USER_DEFINED_FIELD_DETAIL_ID"));
             tmpDmsDocumentDetail.setFieldValue(getResultSetString(rs, "FIELD_VALUE"));
             tmpDmsDocumentDetail.setDateValue(getResultSetTimestamp(rs, "DATE_VALUE"));
             tmpDmsDocumentDetail.setNumericValue(getResultSetDouble(rs, "NUMERIC_VALUE"));
             tmpDmsDocumentDetail.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
             tmpDmsDocumentDetail.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
             tmpDmsDocumentDetail.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
             tmpDmsDocumentDetail.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
             tmpDmsDocumentDetail.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
             tmpDmsDocumentDetail.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
             tmpDmsDocumentDetail.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getCreatorID()));
             tmpDmsDocumentDetail.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getUpdaterID()));
             result.add(tmpDmsDocumentDetail);
           }

        }catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
        } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
        } finally {
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        }

      }
      return result;
    }
   
  /**
   * Gets the document detail object by user defined ID
   * i.e. the document detail get not contains the default info like creator, create Date etc
   * 
   * @param userDefinedID the user defined field ID
   * @return list of the dmsDocument Detail
   * @throws ApplicationException
   * @see com.dcivision.framework.bean.SysUserDefinedIndex
   * 
   */
  public List getDummyDetailByDinstinctDocumentID(Integer userDefinedID) throws ApplicationException{
        PreparedStatement preStat=null;
        ResultSet rs = null;
        StringBuffer sqlStat = new StringBuffer();
        List result = new ArrayList();

        synchronized(dbConn){
          try{
            sqlStat.append("SELECT DISTINCT(A.DOCUMENT_ID) ");
            sqlStat.append("FROM DMS_DOCUMENT_DETAIL A ");
            sqlStat.append("WHERE  A.USER_DEFINED_FIELD_ID=? AND A.RECORD_STATUS<>? ");
            
            preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            this.setPrepareStatement(preStat, 1, userDefinedID);
            this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_INACTIVE);
            rs = preStat.executeQuery();
            while (rs.next()) {
               DmsDocumentDetail tmpDmsDocumentDetail = new DmsDocumentDetail();
               //tmpDmsDocumentDetail.setID(getResultSetInteger(rs, "ID"));
               tmpDmsDocumentDetail.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
               tmpDmsDocumentDetail.setUserDefinedFieldID(userDefinedID);
               tmpDmsDocumentDetail.setUserDefinedFieldDetailID(null);
               tmpDmsDocumentDetail.setFieldValue(null);
               tmpDmsDocumentDetail.setDateValue(null);
               tmpDmsDocumentDetail.setNumericValue(null);
               //tmpDmsDocumentDetail.setRecordStatus(GlobalConstant.RECORD_STATUS_ACTIVE);
               //tmpDmsDocumentDetail.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
               //tmpDmsDocumentDetail.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
               //tmpDmsDocumentDetail.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
               //tmpDmsDocumentDetail.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
               //tmpDmsDocumentDetail.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
               //tmpDmsDocumentDetail.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getCreatorID()));
               //tmpDmsDocumentDetail.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getUpdaterID()));
               result.add(tmpDmsDocumentDetail);
             }

          }catch (SQLException sqle) {
            log.error(sqle, sqle);
            throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
          } catch (Exception e) {
            log.error(e, e);
            throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
          } finally {
            try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
          }

        }
        return result;
      }
  
  
  /**
   * Gets the abstract dms document object by field value.
   * 
   * @param fieldValue the string type field value
   * @return abstract dms document object matched.
   * @throws ApplicationException
   */
  public synchronized AbstractBaseObject getObjectByFieldValue(String fieldValue) throws ApplicationException {
    PreparedStatement ps = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List resultList = new ArrayList();
    DmsDocumentDetail tmpDmsDocumentDetail  = null;
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.USER_DEFINED_FIELD_ID, A.USER_DEFINED_FIELD_DETAIL_ID, A.FIELD_VALUE, A.DATE_VALUE, A.NUMERIC_VALUE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_DOCUMENT_DETAIL A ");
        sqlStat.append("WHERE  A.FIELD_VALUE = ? AND A.RECORD_STATUS = ? ");
        ps = dbConn.prepareStatement(sqlStat.toString());
        setPrepareStatement(ps, 1, fieldValue);
        setPrepareStatement(ps, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = ps.executeQuery();
        if (rs.next()) {
          tmpDmsDocumentDetail  = new DmsDocumentDetail();
          tmpDmsDocumentDetail.setID(getResultSetInteger(rs, "ID"));
          tmpDmsDocumentDetail.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsDocumentDetail.setUserDefinedFieldID(getResultSetInteger(rs, "USER_DEFINED_FIELD_ID"));
          tmpDmsDocumentDetail.setUserDefinedFieldDetailID(getResultSetInteger(rs, "USER_DEFINED_FIELD_DETAIL_ID"));
          tmpDmsDocumentDetail.setFieldValue(getResultSetString(rs, "FIELD_VALUE"));
          tmpDmsDocumentDetail.setDateValue(getResultSetTimestamp(rs, "DATE_VALUE"));
          tmpDmsDocumentDetail.setNumericValue(getResultSetDouble(rs, "NUMERIC_VALUE"));
          tmpDmsDocumentDetail.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsDocumentDetail.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsDocumentDetail.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsDocumentDetail.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsDocumentDetail.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsDocumentDetail.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsDocumentDetail.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getCreatorID()));
          tmpDmsDocumentDetail.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getUpdaterID()));
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { ps.close(); } catch (Exception ignore) {} finally { ps = null; }
      }
    }

    return tmpDmsDocumentDetail;
  }

  public boolean getByUDFIDFieldValue(Integer userDefinedID,String fieldValue) throws ApplicationException{
    PreparedStatement preStat=null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    boolean result = false;

    synchronized(dbConn){
      try{
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.USER_DEFINED_FIELD_ID, A.USER_DEFINED_FIELD_DETAIL_ID, A.FIELD_VALUE, A.DATE_VALUE, A.NUMERIC_VALUE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM DMS_DOCUMENT_DETAIL A, DMS_DOCUMENT D ");
        sqlStat.append("WHERE A.DOCUMENT_ID = D.ID AND A.USER_DEFINED_FIELD_ID=? AND D.RECORD_STATUS<>? ");
        sqlStat.append("AND A.FIELD_VALUE = ?");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, userDefinedID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_INACTIVE);
        this.setPrepareStatement(preStat,3,fieldValue);
        rs = preStat.executeQuery();
        if (rs.next()){
       	 result = true;
        }
      }catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }

    }
    return result;
  }
  
  public synchronized AbstractBaseObject insertOnly(AbstractBaseObject obj) throws ApplicationException {
    return this.insert(obj);
  }
  
  public synchronized List getListByDocumentIDList(List documentIds) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.USER_DEFINED_FIELD_ID, A.USER_DEFINED_FIELD_DETAIL_ID, A.FIELD_VALUE, A.DATE_VALUE, A.NUMERIC_VALUE, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_DOCUMENT_DETAIL A ");
        sqlStat.append("WHERE  A.DOCUMENT_ID IN (");
        for (int i=0; i<documentIds.size(); i++) {
          sqlStat.append(i!=0?",?":"?");
        }
        sqlStat.append(") ");
        sqlStat.append("ORDER BY A.DOCUMENT_ID");
        preStat = dbConn.prepareStatement(sqlStat.toString());
        int counter = 1;
        for (int i=0; i<documentIds.size(); i++) {
          this.setPrepareStatement(preStat, counter++, (Integer)documentIds.get(i));
        }
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsDocumentDetail tmpDmsDocumentDetail = new DmsDocumentDetail();
          tmpDmsDocumentDetail.setID(getResultSetInteger(rs, "ID"));
          tmpDmsDocumentDetail.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsDocumentDetail.setUserDefinedFieldID(getResultSetInteger(rs, "USER_DEFINED_FIELD_ID"));
          tmpDmsDocumentDetail.setUserDefinedFieldDetailID(getResultSetInteger(rs, "USER_DEFINED_FIELD_DETAIL_ID"));
          tmpDmsDocumentDetail.setFieldValue(getResultSetString(rs, "FIELD_VALUE"));
          tmpDmsDocumentDetail.setDateValue(getResultSetTimestamp(rs, "DATE_VALUE"));
          tmpDmsDocumentDetail.setNumericValue(getResultSetDouble(rs, "NUMERIC_VALUE"));
          tmpDmsDocumentDetail.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsDocumentDetail.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsDocumentDetail.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsDocumentDetail.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsDocumentDetail.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsDocumentDetail.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsDocumentDetail.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getCreatorID()));
          tmpDmsDocumentDetail.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDocumentDetail.getUpdaterID()));
          result.add(tmpDmsDocumentDetail);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
    return result;
  }
}


